package com.bmoblistview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class EditActivity extends AppCompatActivity {
    private EditText etName;
    private EditText etAge;
    private RadioButton rbBoy;
    private RadioButton rbGirl;
    private Button btnEtStu;
    private String objectId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        setTitle("编辑学生");

        etName = findViewById(R.id.et_name);
        etAge = findViewById(R.id.et_age);
        rbBoy = findViewById(R.id.rb_boy);
        rbGirl = findViewById(R.id.rb_girl);
        btnEtStu = findViewById(R.id.btn_etStu);

        initViewData();

        saveData();

    }

    private void saveData() {
        btnEtStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = etName.getText().toString();
                int age = Integer.parseInt(etAge.getText().toString());
                String gender = "男";
                if (rbBoy.isChecked()) {
                    gender = "男";
                } else if (rbGirl.isChecked()) {
                    gender = "女";
                }

                Intent intent = new Intent();
                intent.putExtra("NAME", name);
                intent.putExtra("AGE", age);
                intent.putExtra("GENDER", gender);
                intent.putExtra("OBJECTID",objectId);
                setResult(3001, intent);
                finish();
            }
        });
    }

    private void initViewData() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("NAME");
        int age = intent.getIntExtra("AGE", 0);
        String gender = intent.getStringExtra("GENDER");
        objectId = intent.getStringExtra("OBJECTID");

        etName.setText(name);
        etAge.setText(String.valueOf(age));
        if (gender.equals("男")) {
            rbBoy.setChecked(true);
        } else if (gender.equals("女")) {
            rbGirl.setChecked(true);
        }
    }
}
